package io.renren.modules.service_controller.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.renren.modules.service_controller.dao.ServiceControllerDao;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.service_controller.entity.ServiceControllerEntity;
import io.renren.modules.service_controller.service.ServiceControllerService;
import io.renren.common.utils.R;

import javax.annotation.Resource;


/**
 * 菜单管理
 * 服务控制的后端接口
 *
 * @author CK
 * @email 779949398@qq.com
 * @date 2021-12-09 19:24:30
 */
@RestController
@RequestMapping("service_controller/servicecontroller")
public class ServiceControllerController extends AbstractController {
    @Autowired
    private ServiceControllerService serviceControllerService;

    @Resource
    private ServiceControllerDao serviceControllerDao;

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("service_controller:servicecontroller:list")
    public R list(){
//        PageUtils page = serviceControllerService.queryPage(params);
        List<ServiceControllerEntity> entities = serviceControllerService.listWithTree();
        System.out.println("执行了tree");
        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
    //@RequiresPermissions("service_controller:servicecontroller:info")
    public R info(@PathVariable("menuId") Long menuId){
		ServiceControllerEntity serviceController = serviceControllerService.getById(menuId);

        return R.ok().put("serviceController", serviceController);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("service_controller:servicecontroller:save")
    public R save(@RequestBody ServiceControllerEntity serviceController){
		serviceControllerService.save(serviceController);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("service_controller:servicecontroller:update")
    public R update(@RequestBody ServiceControllerEntity serviceController){
        System.out.println(serviceController.getMenuId());
		serviceControllerService.updateById(serviceController);

        return R.ok();
    }

    /**
     * 开启
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("service_controller:servicecontroller:delete")
    public R delete(@RequestBody Integer[] menuIds){
        //System.out.println(Arrays.asList(menuIds));
		//serviceControllerService.removeByIds(Arrays.asList(menuIds));
        List<Integer> n = Arrays.asList(menuIds);
        for (int i = 0; i <n.size() ; i++) {
            System.out.println(n.get(i));
            int taget = serviceControllerDao.getstate(n.get(i));
            if(taget == 1){
                return R.error("该服务已开启，请勿重复开启");
            }else{
                serviceControllerDao.UpdateState(n.get(i));
            }

        }
        return R.ok();
    }

    /**
     * 关闭
     */
    @RequestMapping("/delete1")
    //@RequiresPermissions("service_controller:servicecontroller:delete")
    public R delete1(@RequestBody Integer[] menuIds){
        //System.out.println(Arrays.asList(menuIds));
        //serviceControllerService.removeByIds(Arrays.asList(menuIds));
        List<Integer> n = Arrays.asList(menuIds);
        for (int i = 0; i <n.size() ; i++) {
            System.out.println(n.get(i));
            int taget = serviceControllerDao.getstate(n.get(i));
            if (taget == 0){
                return R.error("该服务已关闭，请勿重复关闭！");
            }else
            {
                serviceControllerDao.UpdateState1(n.get(i));
            }

        }
        return R.ok();
    }
    @RequestMapping("/Geturl")
    //@RequiresPermissions("service_controller:servicecontroller:delete")
    public R Geturl(@RequestBody Integer menuId){
        System.out.println(menuId);
        int count = 49 + menuId;
        String url = "/i-"+count;
        System.out.println(url);
        return R.ok().put("url",url);
    }


/*
* 启动状态
* */
    @PostMapping("/ShowStatus1/{menu_code}")
    public void ShowStatus1(@PathVariable String menu_code ){
        serviceControllerDao.UpdateState_code1(menu_code);
    }

    @PostMapping("/ShowStatus2/{menu_code}")
    public void ShowStatus2(@PathVariable String menu_code ){
        serviceControllerDao.UpdateState_code2(menu_code);
    }

    @GetMapping("/GetStastic/{menu_code}")
    public R get_coloAndExam(@PathVariable String menu_code){
        Map<String, Object> list=serviceControllerDao.get_coloAndExam(menu_code);

        List<Object> list1 =new ArrayList<>();
        for(String i : list.keySet()){
            System.out.println(i);
            System.out.println(list.get(i));

            Object o=new Object(){
                @JsonProperty("name")
                private String name=i;

                @JsonProperty("value")
                private int value= (int) list.get(i);
            };
            list1.add(o);

        }
//        System.out.println(list1);


     return   R.ok().put("data",list1);
    }

}













//    @GetMapping("/list")
//    public List<ServiceControllerEntity> list(){
//        List<ServiceControllerEntity> menuList = serviceControllerService.list();
//        HashMap<Long, ServiceControllerEntity> menuMap = new HashMap<>(12);
//        for (ServiceControllerEntity s : menuList) {
//            menuMap.put(s.getMenuId(), s);
//        }
//        for (ServiceControllerEntity s : menuList) {
//            ServiceControllerEntity parent = menuMap.get(s.getParentId());
//            if (Objects.nonNull(parent)) {
//                s.setParentName(parent.getName());
//            }
//
//        }
//
//
//        return menuList;
//    }



